package com.wb.mas.ui.auth;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.wb.mas.R;
import com.wb.mas.app.AppApplication;
import com.wb.mas.app.Constants;
import com.wb.mas.app.SPKeys;
import com.wb.mas.ui.main.MainActivity;
import com.wb.mas.ui.main.MainViewModel;
import com.wb.mas.ui.success.SuccessActivity;

import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;

public class AuthJumpManager {

    private static final String TAG = AuthJumpManager.class.getSimpleName();

    public static boolean jumpToNextPage(String currentPageId) {


        if (TextUtils.isEmpty(currentPageId)) {
            gotoMainPage();
            return false;
        }
        String noAuthStr = SPUtils.getInstance().getString(SPKeys.SP_NOAUTH_STEP);
        KLog.i(TAG, noAuthStr);

        if (TextUtils.isEmpty(noAuthStr)) {
            gotoMainPage();
            return false;

        }

        String[] authArray = noAuthStr.split(",");
        if (authArray == null) {
            gotoMainPage();
            return false;
        }

        int currentIndex = -1;
        for (int i = 0; i < authArray.length; i++) {
            String itemAuthCode = authArray[i];
            if (TextUtils.isEmpty(itemAuthCode)) {
                continue;
            }
            if (itemAuthCode.equals(currentPageId)) {
                currentIndex = i;
                break;
            }

        }
        if (currentIndex == -1) {
            gotoMainPage();
            return false;
        }

        if (currentIndex == authArray.length - 1) {//所有的认证步骤都完成了
            Messenger.getDefault().send("", MainViewModel.AUTH_SUC_EVENT);
            goAuthSucPage();
            KLog.i(TAG, "all auth step is over");
            return true;
        }

        currentIndex += 1;
        if (currentIndex >= 0 && currentIndex < authArray.length) {

            String nextStepCode = authArray[currentIndex];
            goToNextPage(nextStepCode);
            return true;
        }


        gotoMainPage();
        return false;
    }

    private static void goAuthSucPage() {
        Bundle bundle = new Bundle();
        bundle.putInt("type", SuccessActivity.TYPE_AUTH);
        Intent intent = new Intent(AppApplication.getInstance(), SuccessActivity.class);
        AppApplication.goActivity(intent);
    }


    /**
     * AUTH_PAGE_CODE_CONTACTS_CODE = "contacts_code";//联系人认证页面
     * String AUTH_PAGE_CODE_PERSONAGE_INFO_CODE = "personage_info_code";//个人信息页面
     * String AUTH_PAGE_CODE_BANK_INFO_CODE = "bank_info_code";//认证银行卡页面
     * String AUTH_PAGE_CODE_COMPANY_CODE = "company_code";//工作认证页面
     * String AUTH_PAGE_CODE_ADDITIONAL_INFORMATION_CODE = "additional_information_code";//额外信息 认证页面
     * String AUTH_PAGE_CODE_IDENTITY_COD
     *
     * @param nextStepCode
     */
    public static void goToNextPage(String nextStepCode) {


        if (Constants.AUTH_PAGE_CODE_CONTACTS_CODE.equals(nextStepCode)) {


            Intent intent = new Intent(AppApplication.getInstance(), AuthContactAc.class);
            AppApplication.goActivity(intent);
        } else if (Constants.AUTH_PAGE_CODE_PERSONAGE_INFO_CODE.equals(nextStepCode)) {
            Intent intent = new Intent(AppApplication.getInstance(), AuthPersonalInfoAc.class);
            AppApplication.goActivity(intent);

        } else if (Constants.AUTH_PAGE_CODE_BANK_INFO_CODE.equals(nextStepCode)) {
            Intent intent = new Intent(AppApplication.getInstance(), AuthBankCardAc.class);
            AppApplication.goActivity(intent);


        } else if (Constants.AUTH_PAGE_CODE_COMPANY_CODE.equals(nextStepCode)) {
            Intent intent = new Intent(AppApplication.getInstance(), AuthWorkInfoAc.class);
            AppApplication.goActivity(intent);


        } else if (Constants.AUTH_PAGE_CODE_ADDITIONAL_INFORMATION_CODE.equals(nextStepCode)) {
            Intent intent = new Intent(AppApplication.getInstance(), AuthExtraAc.class);
            AppApplication.goActivity(intent);


        } else if (Constants.AUTH_PAGE_CODE_IDENTITY_COD.equals(nextStepCode)) {
            Intent intent = new Intent(AppApplication.getInstance(), AuthIdAc.class);
            AppApplication.goActivity(intent);


        }
    }

    private static void gotoMainPage() {
        Intent intent = new Intent(AppApplication.getInstance().getApplicationContext(), MainActivity.class);
        AppApplication.goActivity(intent);

    }


    public static String getPageTitle(Context context
            , String pageCode) {

        if (Constants.AUTH_PAGE_CODE_CONTACTS_CODE.equalsIgnoreCase(pageCode)) {
            return context.getResources().getString(R.string.auth_contact_page_tile);
        }
        if (Constants.AUTH_PAGE_CODE_PERSONAGE_INFO_CODE.equalsIgnoreCase(pageCode)) {
            return context.getResources().getString(R.string.auth_pi_page_title_name);
        }
        if (Constants.AUTH_PAGE_CODE_BANK_INFO_CODE.equalsIgnoreCase(pageCode)) {
            return context.getResources().getString(R.string.auth_bank_card_page_title);
        }
        if (Constants.AUTH_PAGE_CODE_COMPANY_CODE.equalsIgnoreCase(pageCode)) {
            return context.getResources().getString(R.string.auth_wi_page_tile);
        }
        if (Constants.AUTH_PAGE_CODE_ADDITIONAL_INFORMATION_CODE.equalsIgnoreCase(pageCode)) {
            return context.getResources().getString(R.string.auth_extra_page_title);
        }
        if (Constants.AUTH_PAGE_CODE_IDENTITY_COD.equalsIgnoreCase(pageCode)) {
            return context.getResources().getString(R.string.auth_page_title_name);
        }
        return "";
    }

}
